<!--  -->
<template>
  <div>
    <el-form ref="form" label-width="100px">
      <el-form-item label="SPU名称">
        {{ spu.spuName }}
      </el-form-item>
      <el-form-item label="SKU名称">
        <el-input v-model="skuInfo.skuName" placeholder="请输入SKU名称" />
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input v-model="skuInfo.price" type="number" placeholder="请输入价格(元)" />
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input v-model="skuInfo.weight" placeholder="请输入重量(千克)" />
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input v-model="skuInfo.skuDesc" type="textarea" rows="4" placeholder="请输入规格描述" />
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form ref="form" :inline="true" label-width="100px">
          <el-form-item v-for="attr in attrInfoList" :key="attr.id" :label="attr.attrName">
            <el-select v-model="attr.attrIdAndValueId" placeholder="请选择">
              <el-option v-for="attrValue in attr.attrValueList" :key="attrValue.id" :value="`${attr.id}:${attrValue.id}`" :label="attrValue.valueName" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form ref="form" :inline="true" label-width="100px">
          <el-form-item v-for="saleAttr in spuSaleAttrList" :key="saleAttr.id" :label="saleAttr.saleAttrName">
            <el-select v-model="saleAttr.attrIdAndValueId" placeholder="请选择">
              <el-option v-for="saleAttrValue in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id" :value="`${saleAttr.id}:${saleAttrValue.id}`" :label="saleAttrValue.saleAttrValueName" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table style="width: 100%" border :data="spuImageList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" prop="prop" width="100px" />
          <el-table-column prop="prop" label="图片" width="width">
            <!-- eslint-disable-next-line -->
            <template slot-scope="{row, $index}">
              <img :src="row.imgUrl" style="width: 100px; height: 100px;" alt="">
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width" />
          <el-table-column prop="prop" label="操作" width="width">
            <!-- eslint-disable-next-line -->
            <template slot-scope="{row, $index}">
              <el-button v-if="row.isDefault == 0" type="primary" @click="setDefault(row)">设置默认</el-button>
              <el-button v-else plain>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveSku">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'SkuForm',
  data() {
    return {
      spuImageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      // 收集sku信息
      skuInfo: {
        // 第一类收集的数据：父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        // 第二类：需要通过数据双向绑定v-model收集
        skuName: '',
        price: 0,
        weight: '',
        skuDesc: '',
        // 第三类：自己收集
        // 默认图片
        skuDefaultImg: '',
        // 收集图片的字段
        skuImageList: [
          // {
          //   id: 0,
          //   imgName: "string",
          //   imgUrl: "string",
          //   isDefault: "string",
          //   skuId: 0,
          //   spuImgId: 0,
          // },
        ],
        // 平台属性
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   valueId: 0,
          // },
        ],
        // 销售属性
        skuSaleAttrValueList: [
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: "string",
          //   saleAttrValueId: 0,
          //   saleAttrValueName: "string",
          //   skuId: 0,
          //   spuId: 0,
          // },
        ]
      },
      spu: {},
      // 临时图片收集
      imageList: []
    }
  },
  methods: {
    async initSkuData(category1Id, category2Id, spu) {
      // 收集父组件传递的数据
      this.skuInfo.category3Id = spu.category3Id
      this.skuInfo.spuId = spu.id
      this.skuInfo.tmId = spu.tmId
      this.spu = spu
      // 获取图片的数据 reqSpuImageLIst
      const imgResult = await this.$API.spu.reqSpuImageLIst(spu.id)
      if (imgResult.code === 200) {
        const tempList = imgResult.data
        tempList.forEach(item => {
          item.isDefault = 0
        })
        this.spuImageList = tempList
      }
      // 获取销售属性的数据 reqSpuSaleAttrList
      const saleResult = await this.$API.spu.reqSpuSaleAttrList(spu.id)
      if (saleResult.code === 200) {
        this.spuSaleAttrList = saleResult.data
      }
      // 获取平台属性的数据 reqAttrInfoList
      const attrResult = await this.$API.spu.reqAttrInfoList(category1Id, category2Id, spu.category3Id)
      if (attrResult.code === 200) {
        this.attrInfoList = attrResult.data
      }
    },
    // 图片选择时事件
    handleSelectionChange(selectedInfo) {
      // selectedInfo传递的是当前勾选图片信息
      // 但和传递向服务器数据格式缺少字段 此处临时存储勾选图片信息
      this.imageList = selectedInfo
    },
    // 默认设置 排它
    setDefault(row) {
      this.spuImageList.forEach(item => {
        item.isDefault = 0
      })
      row.isDefault = 1
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    cancel() {
      // 通知父组件 切换场景
      this.$emit('changeSceneZero', 0)
      Object.assign(this._data, this.$options.data())
    },
    // 保存sku
    async saveSku() {
      const { attrInfoList, skuInfo, spuSaleAttrList, imageList } = this
      // 方案1
      // const tempArr = []
      // attrInfoList.forEach(item => {
      //   // 选择了平台属性 数据分离
      //   if (item.attrIdAndValueId) {
      //     const [attrId, valueId] = item.attrIdAndValueId.split(':')
      //     const obj = { attrId, valueId }
      //     obj.push(tempArr)
      //   }
      // })
      // this.skuInfo.skuAttrValueList = tempArr

      // 2
      // 平台属性
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(':')
          prev.push({ attrId, valueId })
        }
        return prev
      }, [])

      // 销售属性 spuSaleAttrList
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':')
          prev.push({ saleAttrId, saleAttrValueId })
        }
        return prev
      }, [])
      // 整理图片的数据
      skuInfo.skuImageList = imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id
        }
      })

      const result = await this.$API.spu.reqAddSku(skuInfo)
      if (result.code === 200) {
        this.$message({ type: 'success', message: '添加SKU成功' })
        this.$emit('changeSceneZero', 0)
        Object.assign(this._data, this.$options.data())
      } else {
        this.$message('添加失败，请稍后再试！')
      }
    }
  }
}
</script>

<style  scoped>

</style>
